﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.ObjectPool;

namespace KalevaAalto.DataPart.Base
{
    internal class SqlSugarClientPooledObjectPolicy : IPooledObjectPolicy<ISqlSugarClient>
    {
        private readonly Func<ISqlSugarClient> _getSqlSugarClient;

        public SqlSugarClientPooledObjectPolicy(Func<ISqlSugarClient> getSqlSugarClient)
        {
            _getSqlSugarClient = getSqlSugarClient;
        }

        public ISqlSugarClient Create()
        {
            return _getSqlSugarClient();
        }

        public bool Return(ISqlSugarClient obj)
        {
            // You can add logic here to determine if the object is reusable.
            // For example, you can check if the connection is still open.
            return obj.Ado.Connection.State == System.Data.ConnectionState.Open;
        }

        public void Dispose(ISqlSugarClient obj)
        {
            // Dispose the object, e.g., release resources
            obj.Dispose();
        }
    }
}
